﻿$(function () {
    var form = $("#login-pop-form");
    form.validate();

    $("#btn_login_pop").click(function () {       
        if (form.valid()) {
            var email = $("#email_pop").val();
            var password = $("#password_pop").val();
            var rememberme = $("#rememberme_pop").prop('checked');
            
            var url = "/login/Pop_login/";
            var data = "{email:'" + email + "',password:'" + password + "',rememberMe:'" + rememberme + "'}";
            jQuery.ajax({
                type: "POST",
                contentType: "application/json",
                url: url,
                data: data,
                dataType: 'json',
                success: function (returnData) {
                    if (returnData["result"] == "success") {
                        var userName = returnData["username"];
                        var loginHtml = '<div class="reg-button welcome-message">WELCOME, <a class="username" href="/Account/Manage" title="Manage">' + userName + '</a></div><div class="borandlogin"><div class="bor-button manage-account"><a class="accountlink" href="/AdminDefault" title="Manage Account">Manage Account</a></div><div class="login-button logoff"><form action="/Login/LogOff" id="logoutForm" method="post" novalidate="novalidate"><input name="__RequestVerificationToken" type="hidden" value="jC84c1y5oUEV-rogztogH_qmCtZZyggg86QQJFQDILKZk__JG3BX2GiTJZVaFb3wPwIVRy0Z0ANvAb9Z2sXIqRa7k2GGEdl_NA1zCyymrG7j8SbIrzeqEAoOq3LJqrzcrrYAjTKFtN2PtxCZaO71QEDKSBLLczECoPEFiLMRnqA1"><a href="javascript:document.getElementById(\'logoutForm\').submit()">Log off</a></form></div></div>';
                        $("#header .login").html("");
                        $("#header .login").append(loginHtml);
                        $.colorbox.close();

                    } else {
                        jAlert(returnData["errormsg"],"Error");
                    }
                }
            });                      
        }
    });
});

function RateHere(rateId) {
    var url = "/login/auth_user/";
    var data = "{}";
    jQuery.ajax({
        type: "POST",
        contentType: "application/json",
        url: url,
        data: data,
        dataType: 'json',
        success: function (returnData) {
            if (returnData["result"] == "success") {                
                $("input[name=" + rateId + "]").rating('readOnly', false);
                $("input[name=" + rateId + "]").rating('select', 9, false);
            } else {
                $.colorbox({ inline: true, href: "#loginform" });
            }
        }
    });
}

function RateSubmit(obj, rate, siteid) {   
    rateObj = $("input[name=" + obj.name + "]");

    var url = "/Site/SubmitRate/";
    var data = "{rate:'" + rate + "',siteId:'" + siteid + "'}";
    jQuery.ajax({
        type: "POST",
        contentType: "application/json",
        url: url,
        data: data,
        dataType: 'json',
        success: function (returnData) {
            if (returnData["result"] == "success") {
                jAlert("Rate Success!", "Success");
            }
            else if (returnData["result"] == "failed") {
                jAlert(returnData["errormsg"], "Failed");
            }
            var rateNum = returnData["newrate"] / 0.5 - 1;
            rateObj.rating('select', rateNum, false);
            rateObj.rating('readOnly', true);
        }
    });
}

